const { SensitiveWordTool } = require('sensitive-word-tool')
const axiso = require('axios')
// 初始化时使用默认敏感词
const sensitiveWordTool = new SensitiveWordTool({
  useDefaultWords: true,
})

async function uFilterSenWords(word) {
  console.log(`output->过滤敏感词`,word)
  let sz = sensitiveWordTool.filter(word)
  let result = await getFliterSensitiveWord(sz)
  if (result.code === 0 && result.data.words.length > 0) {
    //替换字符串中的敏感词
    sensitiveWordTool.addWords(result.data.words)
    let filterText = sensitiveWordTool.filter(result.data.text)
    return filterText
  }
  return result.data.text
}

function getFliterSensitiveWord(sz) {
  return new Promise(async (resolve, reject) => {
    try {
      let appid = await axiso.post('https://luckycola.com.cn/ai/getColaKey', {
        uid: 'ptlwCB1709451106846kkltrmGybN',
        appKey: '65e42772a1c77eb468923678',
      })
      if (appid.data.code !== 0) {
        console.log('appid获取失败')
        return
      }
      // console.log(appid.data, 'appid')
      // resolve(appid)
      let result = await axiso.post('https://luckycola.com.cn/tools/sensiWords', {
        ColaKey: appid.data.data.cola_key,
        isStrict: '1',
        wordStr: sz,
      })
      resolve(result.data)
    } catch (e) {
      resolve(e)
    }
  })
}
module.exports = {
  uFilterSenWords,
}
